package com.fosu.service.Util;

import com.fosu.Utils.RedisUtil;
import com.alibaba.fastjson.JSON;
import com.fosu.common.ResponseResult;
import com.fosu.dao.UserDao;
import com.fosu.domain.User;

import com.fosu.vo.UserDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * @author zqa
 * @version 1.0
 * @date 2020/11/10 10:02
 */
@Component
public class GetUserByTokenUtil {
    @Autowired
    UserDao userDao;
    @Autowired
    RedisUtil redisUtil;

    public User getUser(HttpServletRequest request, HttpServletResponse response) {
        String authHeader = request.getHeader("Authorization");//获取请求头的信息  请求头的key叫Authorization
        response.setCharacterEncoding("utf-8");//设置网页字符集UTF-8
        if (authHeader == null || !authHeader.startsWith("Bearer ")) {
            return null;
        }
        String authToken = authHeader.substring("Bearer ".length());
        System.out.println(authToken);

        HashMap<String, Object> hashMap = (HashMap<String, Object>) redisUtil.hget(authToken);
        User user = userDao.findUserByOpenId(String.valueOf(hashMap.get("id")));
        return user;
    }
}
